package com.darren.euc.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public final class SerializeUtil { 

	/**
	 * 序列化对象
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] serializeObject(Object object) throws IOException {		
		byte[] byteArray = null;
		if(object != null){
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			oos.flush();
			oos.close();
			byteArray = baos.toByteArray();
			baos.close();
		}
		return byteArray;
	}

    


    /**
     * 反序列化对象
     * @param buffer
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object deserializeObject(byte[] byteArray) throws IOException,ClassNotFoundException{
       Object object=null; 
       if(byteArray != null && byteArray.length > 0){
    	   ByteArrayInputStream bais=new ByteArrayInputStream(byteArray);
           ObjectInputStream ois = new ObjectInputStream(bais); 
           object= ois.readObject(); 
           ois.close();
           bais.close();
       }       
       return object; 
    } 

} 
